let dao = require('../database/dao'),
  logger = require('../helper/logger'),
  tagLogger = new logger.TagLogger('COMMENT_MODEL'),
  errorCode = require('../../conf/errorCode'),
  commonModel = require('./commonModel'),
  util = require('../helper/util'),
  _ = require('lodash');

let {
  genUpdateCallback,
  genInsertCallback,
  genUpdateRslt,
  genUpdateBatchRslt,
  genUpdateBatchCallback,
  genSelectCallback,
  genPagination,
  genInertRslt,
} = commonModel;


//根据postId获取评论列表
exports.getCommentByPostId = function(postId, page, size, callback) {
  let [offset, limit] = util.ps2ol(page, size),
    fromJoinWhere = `FROM comments as c 
                        LEFT JOIN user as u
              		    ON c.uid = u.id 
                                where c.status >= 0 and object_id=${postId}
                        `;

  let postQuery = {
    str: `SELECT u.id userId,u.accountName nickName,u.avatarUrl avatarUrl,c.id id,c.content content,c.ctime createTime ${fromJoinWhere} order by c.ctime desc LIMIT ?,? `,
    param: [offset, limit],
  };
  let countQuery = {
    str: `SELECT count(1) as count ${fromJoinWhere}`,
    param: [],
  };
  let queryArr = [postQuery, countQuery];
  dao.queryBatch(queryArr, (err, rslts) => {
    if (err == null)
      callback(null, genPagination(rslts[0], page, size, rslts[1][0].count));
    else callback(err, null);
  });
};

//删除评论
exports.deleteCommentByIds = function(ids, callback) {
  dao.deleteBatch('comments', ids, callback);
};

